﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Hr;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using ESOL.WinUI.UICommon;

namespace ESOL.WinUI.Hr
{
    public partial class frmMaintainRenewContract : BaseForm.frmBaseDialogyBusinessForm
    {
        #region Variable
        private MstPersonEntity mSelectedPerson;
        private HrContractHistoryEntity mSelectedContract;
        #endregion

        #region Constructor
        public frmMaintainRenewContract(HrContractHistoryEntity entity)
        {
            InitializeComponent();
            mSelectedContract = entity;
        }

        public frmMaintainRenewContract(MstPersonEntity person)
        {
            InitializeComponent();
            mSelectedPerson = person;
        }

        public frmMaintainRenewContract()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void SetPersonFields()
        {
            txtPerson.Text = "";
            txtPersonNameCn.Text = "";
            cmbStatus.SelectedIndex = -1;
            txtMobile.Text = "";
            if (mSelectedPerson == null)
            {
                return;
            }
            txtPerson.Text = mSelectedPerson.PersonCode;
            txtPersonNameCn.Text = mSelectedPerson.NameCn;
            cmbStatus.SetSelectedValue(mSelectedPerson.Status);
            txtMobile.Text = mSelectedPerson.Mobile;
            HrEmployeeEntity employee = HrEmployeeBs.GetHrEmployeeByPersonId(mSelectedPerson.PersonId);
            if (employee != null && employee.ContractEndDate.ToString("yyyy-MM-dd") != "0001-01-01" && employee.ContractEndDate.ToString("yyyy-MM-dd") != "1900-01-01")
            {
                dtpOriginalEndDate.EditValue = employee.ContractEndDate;
            }
        }

        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "STATUS", false);
        }

        private void SetFields()
        {
            if (mSelectedContract == null)
            {
                return;
            }
            if (string.IsNullOrEmpty(mSelectedContract.PersonId) == false)
            {
                mSelectedPerson = MstPersonBs.GetMstPersonByID(mSelectedContract.PersonId);
                SetPersonFields();
            }
            if (mSelectedContract.ContractDate.ToString("yyyy-MM-dd") != "1900-01-01"&&
                 mSelectedContract.ContractDate.ToString("yyyy-MM-dd") != "0001-01-01")
            {
                dtpContractDate.EditValue = mSelectedContract.ContractDate;
            }
            if (mSelectedContract.ContractStartDate.ToString("yyyy-MM-dd") != "1900-01-01" &&
                 mSelectedContract.ContractStartDate.ToString("yyyy-MM-dd") != "0001-01-01")
            {
                dtpContractStartDate.EditValue = mSelectedContract.ContractStartDate;
            }
            if (mSelectedContract.ContractEndDate.ToString("yyyy-MM-dd") != "1900-01-01" &&
                 mSelectedContract.ContractEndDate.ToString("yyyy-MM-dd") != "0001-01-01")
            {
                dtpContractEndDate.EditValue = mSelectedContract.ContractEndDate;
            }
            cmbStatus.SetSelectedValue(mSelectedContract.Status);
            txtContractCode.Text = mSelectedContract.ContractCode;
            txtRemark.Text = mSelectedContract.Remark;

            UICommonFunctions.SetTextBoxDate(txtCreatedDate, mSelectedContract.CreatedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtCreatedBy, mSelectedContract.CreatedBy);
            UICommonFunctions.SetTextBoxDate(txtLastModifiedDate, mSelectedContract.LastModifiedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtLastModifiedBy, mSelectedContract.LastModifiedBy);
            UICommonFunctions.SetTextBoxDate(txtApprovedDate, mSelectedContract.ApprovedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtApprovedBy, mSelectedContract.ApprovedBy);
        }

        private void SetEntity()
        {
            if (mSelectedContract == null)
            {
                mSelectedContract = new HrContractHistoryEntity();
                mSelectedContract.ContractHistoryId = Guid.NewGuid().ToString();
            }
            if (mSelectedPerson != null)
            {
                mSelectedContract.PersonId = mSelectedPerson.PersonId;
            }
            if (dtpContractDate.EditValue!=null)
            {
                mSelectedContract.ContractDate = Convert.ToDateTime(dtpContractDate.EditValue);
            }
            if (dtpContractStartDate.EditValue != null)
            {
                mSelectedContract.ContractStartDate = Convert.ToDateTime(dtpContractStartDate.EditValue);
            }
            if (dtpContractEndDate.EditValue != null)
            {
                mSelectedContract.ContractEndDate = Convert.ToDateTime(dtpContractEndDate.EditValue);
            }
            mSelectedContract.Status = "STATUS02";
            mSelectedContract.ContractCode = txtContractCode.Text;
            mSelectedContract.Remark = txtRemark.Text;

            if (ThisActionType == ActionType.New)
            {
                mSelectedContract.CreatedDate = DateTime.Now;
                mSelectedContract.CreatedBy = CommonParameter.CurrentUser.PersonId;
            }
            if (ThisActionType == ActionType.New || ThisActionType == ActionType.Modify)
            {
                mSelectedContract.LastModifiedDate = DateTime.Now;
                mSelectedContract.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
            }
        }

        private bool CheckData()
        {
            if (mSelectedPerson == null)
            {
                MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtContractCode.Text))
            {
                MessageBox.Show("合同编号不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (dtpContractDate.EditValue == null)
            {
                MessageBox.Show("合同日期不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (dtpContractStartDate.EditValue == null)
            {
                MessageBox.Show("合同开始日期不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (dtpContractEndDate.EditValue == null)
            {
                MessageBox.Show("合同终止日期不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (Convert.ToDateTime(dtpContractEndDate.EditValue) < Convert.ToDateTime(dtpContractStartDate.EditValue))
            {
                MessageBox.Show("合同终止日期不能小于合同开始日期！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }

        private bool Save()
        {
            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                if (ThisActionType == ActionType.New)
                {
                    SetEntity();
                    bool isSuccessful = HrContractHistoryBs.InsertHrContractHistory(mSelectedContract);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("新增失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Modify)
                {
                    SetEntity();
                    bool isSuccessful = HrContractHistoryBs.UpdateHrContractHistory(mSelectedContract);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Approve)
                {
                    mSelectedContract.ApprovedDate = DateTime.Now;
                    mSelectedContract.ApprovedBy = CommonParameter.CurrentUser.PersonId;
                    bool isSuccessful = HrContractHistoryBs.ApproveHrContractHistory(mSelectedContract);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("审核失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        #endregion

        #region Event
        private void frmMaintainRenewContract_Load(object sender, EventArgs e)
        {
            BindComboBox();
            SetPersonFields();
            SetFields();
        }

        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        protected override void Click_Approve()
        {
            if (mSelectedContract == null)
            {
                return;
            }
            mSelectedContract.Status = "STATUS03";
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        protected override void Click_Reject()
        {
            if (mSelectedContract == null)
            {
                return;
            }
            mSelectedContract.Status = "STATUS04";
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void txtPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                mSelectedPerson = frm.SelectedPerson;
                SetPersonFields();
            }
            else
            {
                mSelectedPerson = null;
                SetPersonFields();
            }
        }
        #endregion
    }
}
